﻿using Dapper;
using System;
using BCEntity.Sim.Company.JobTitle;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Sim.Company.JobTitle
{
	/// <summary>
	/// 职位
	/// </summary>
	public class JobTitleData : IJobTitleData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM jobtitle";
		private const string QUERY = @"SELECT JobTitleId,EditAdminName,UpdateTime,JobTitleNo,JobTitle,Enabled,JobTitleRemark,CreateAdminId,CreateAdminName,CreateTime,EditAdminId FROM jobtitle";
		public JobTitleData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查职位是否存在
		public async Task<bool> Exists(int jobTitleId)
		{
			string sql = string.Format(@"{0} WHERE JobTitleId=?JobTitleId LIMIT 1", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?JobTitleId", jobTitleId, DbType.Int32);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		public async Task<bool> Exists(string jobTitleNo)
		{
			string sql = string.Format(@"{0} WHERE JobTitleNo=?JobTitleNo LIMIT 1", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?JobTitleNo", jobTitleNo, DbType.AnsiString);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加职位
		public async Task<JobTitleEntity> Add(JobTitleEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("jobtitle", new string[] { "JobTitleNo", "JobTitle", "CreateAdminId", "CreateAdminName", });
			sql = string.Format("{0} {1} WHERE JobTitleId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?JobTitleNo", entity.JobTitleNo?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?JobTitle", entity.JobTitle?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<JobTitleEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改职位
		public async Task<JobTitleEntity> Update(JobTitleEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("jobtitle", new string[] { "EditAdminName", "JobTitleNo", "JobTitle", "JobTitleRemark", "EditAdminId" }, new string[] { "JobTitleId" });
			sql = string.Format("{0} {1} WHERE JobTitleId=?JobTitleId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?JobTitleId", entity.JobTitleId, DbType.Int32, size: 11);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?JobTitleNo", entity.JobTitleNo?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?JobTitle", entity.JobTitle?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?JobTitleRemark", entity.JobTitleRemark?.Trim(), DbType.AnsiString, size: 400);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<JobTitleEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除职位
		public async Task<bool> Delete(int jobTitleId)
		{
			string sql = @"DELETE FROM jobtitle WHERE JobTitleId=?JobTitleId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?JobTitleId", jobTitleId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取职位
		public async Task<JobTitleEntity> GetEntity(int jobTitleId)
		{
			string sql = string.Format(@"{0} WHERE JobTitleId=?JobTitleId LIMIT 1", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?JobTitleId", jobTitleId, DbType.Int64);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<JobTitleEntity>(sql, parameters);
		}
		public async Task<JobTitleEntity> GetEntity(string jobTitleNo)
		{
			string sql = string.Format(@"{0} WHERE JobTitleNo=?JobTitleNo LIMIT 1", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?JobTitleNo", jobTitleNo, DbType.AnsiString);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<JobTitleEntity>(sql, parameters);
		}
		#endregion

		#region 获取职位列表
		public async Task<IEnumerable<JobTitleEntity>> GetList()
		{
			string sql = string.Format(@"{0} WHERE Enabled=1", QUERY);
			return await this.databaseContext.Slave.QueryAsync<JobTitleEntity>(sql);
		}
		#endregion

		#region 获取职位分页
		public async Task<(IEnumerable<JobTitleEntity>, int)> Query(string jobTitle,bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();
			if (!string.IsNullOrWhiteSpace(jobTitle))
			{
				wheres.Add("JobTitle LIKE CONCAT('%',CONCAT(?JobTitle,'%'))");
				parameters.Add("?JobTitle", jobTitle.Trim(), DbType.AnsiString);
			}
			if (enabled.HasValue)
			{
				wheres.Add("Enabled=?Enabled");
				parameters.Add("?Enabled", enabled.Value, DbType.Boolean);
			}
			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM jobtitle {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.Slave.QueryMultipleAsync<JobTitleEntity, int>(sql, parameters);
		}
		#endregion

		#region 启用职位
		public async Task<bool> Enabled(int jobTitleId)
		{
			string sql = @"UPDATE jobtitle SET Enabled=1 WHERE JobTitleId=?JobTitleId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?JobTitleId", jobTitleId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 禁用职位
		public async Task<bool> Disabled(int jobTitleId)
		{
			string sql = @"UPDATE jobtitle SET Enabled=0 WHERE JobTitleId=?JobTitleId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?JobTitleId", jobTitleId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion
	}
}



